<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: DBBrowser.php 375 2010-09-10 08:13:21Z steen.rabol $
 *
 */
Prado::using('Application.Common.Data.PortalLanguageStringRecord');
class DBBrowser extends PortalAdminPage
{
	public static function ToolMenuName()
	{
		return Prado::localize('DB Browser');
	}

	public function onLoad($param)
	{        
		parent::onLoad($param);
		$this->setAdminPageTitle("DB browser");

		if(!$this->IsPostBack)
		{
            if($this->Request['command'] == null)
            {
                $this->pnlTables->Visible   = true;
                $this->pnlTables->Enabled   = true;
                $this->dlDbTables->dataSource = $this->Tables;
                $this->dlDbTables->dataBind();
            }
            else if($this->Request['command'] == 'viewtable')
            {
                //$this->DisplayTableData($this->Request['table']);
                $this->pnlTableData->Visible    = true;
                $this->pnlTableData->Enabled    = true;
                $this->DataGrid->dataSource  = $this->Data;
                $this->DataGrid->dataBind();
            }
        
		}
	}


    public function getData()
    {
        return $this->DataAccess->createCommand("select * from " . $this->Request['table'])->query();
    }

    public function getTables()
    {
   		$status = array();
		//$status['total_size'] = 0;
        $db_name = $this->Application->Parameters['DbDatabase'];
		$result = $this->DataAccess->createCommand("SHOW TABLE STATUS FROM `$db_name`")->query();
        foreach($result as $row)
		{
			//$status['total_size'] += $row['Data_length']; // + Index_length
			//$status[$row['Name']]['size'] = $row['Data_length'];
			//$status[$row['Name']]['count'] = $row['Rows'];
            $status[] = array('id' => $row['Name'],'size' => $row['Data_length'],'count'=>$row['Rows']);
		}

        return $status;
    }

    public function changePage($sender,$param)
    {
        $this->DataGrid->CurrentPageIndex=$param->NewPageIndex;
        $this->DataGrid->DataSource=$this->Data;
        $this->DataGrid->dataBind();
    }

    public function pagerCreated($sender,$param)
    {
        $param->Pager->Controls->insertAt(0,'Page: ');
    }

    public function changePagerPosition($sender,$param)
    {
        $top=$sender->Items[0]->Selected;
        $bottom=$sender->Items[1]->Selected;
        if($top && $bottom)
            $position='TopAndBottom';
        else if($top)
            $position='Top';
        else if($bottom)
            $position='Bottom';
        else
            $position='';
        if($position==='')
            $this->DataGrid->PagerStyle->Visible=false;
        else
        {
            $this->DataGrid->PagerStyle->Position=$position;
            $this->DataGrid->PagerStyle->Visible=true;
        }
    }

    public function useNumericPager($sender,$param)
    {
        $this->DataGrid->PagerStyle->Mode='Numeric';
        $this->DataGrid->PagerStyle->NextPageText=$this->NextPageText->Text;
        $this->DataGrid->PagerStyle->PrevPageText=$this->PrevPageText->Text;
        $this->DataGrid->PagerStyle->PageButtonCount=$this->PageButtonCount->Text;
        $this->DataGrid->DataSource=$this->Data;
        $this->DataGrid->dataBind();
    }

    public function useNextPrevPager($sender,$param)
    {
        $this->DataGrid->PagerStyle->Mode='NextPrev';
        $this->DataGrid->PagerStyle->NextPageText=$this->NextPageText->Text;
        $this->DataGrid->PagerStyle->PrevPageText=$this->PrevPageText->Text;
        $this->DataGrid->DataSource=$this->Data;
        $this->DataGrid->dataBind();
    }

    public function changePageSize($sender,$param)
    {
        $this->DataGrid->PageSize=TPropertyValue::ensureInteger($this->PageSize->Text);
        $this->DataGrid->CurrentPageIndex=0;
        $this->DataGrid->DataSource=$this->Data;
        $this->DataGrid->dataBind();
    }



}
?>